package com.xyzwps.harbor.repo;

import com.xyzwps.harbor.model.User;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Mono;

@Repository
public interface UserRepository extends ReactiveCrudRepository<User, Long> {

    @Query("select user_id, username, avatar, nick_name, register_time, status from users where user_id = :userId" )
    Mono<User> selectByUserId(long userId);

    @Query(""" 
             select user_id, username, avatar, nick_name, register_time, status
             from users
             where username = :username 
            """)
    Mono<User> selectByUsername(String username);


    @Modifying
    @Query("""
            insert into users
            set user_id       = :#{[0].userId},
                username      = :#{[0].username},
                avatar        = :#{[0].avatar},
                nick_name     = :#{[0].nickName},
                register_time = :#{[0].registerTime},
                status        = :#{[0].status}
             """)
    Mono<Void> insert(User user);

}